package com.ex.mzwqz;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.android.volley.RequestQueue;
import com.android.volley.cache.plus.BitmapImageCache;
import com.android.volley.cache.plus.SimpleImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by xiaguoqiang on 16/10/20.
 */
public class AppContext extends Application {

    private static AppContext appContext;
    private RequestQueue mRequestQueue;
    private SimpleImageLoader mImageLoader;

    public static AppContext getInstance() {
        return appContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;
        mRequestQueue = Volley.newRequestQueue(this);
        mImageLoader = new SimpleImageLoader(mRequestQueue, BitmapImageCache.getInstance(null));
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue != null) {
            return mRequestQueue;
        } else {
            throw new IllegalStateException("RequestQueue not initialized");
        }
    }

    /**
     * Returns instance of ImageLoader initialized with {@see FakeImageCache} which effectively means
     * that no memory caching is used. This is useful for images that you know that will be show
     * only once.
     *
     * @return
     */
    public SimpleImageLoader getImageLoader() {
        if (mImageLoader != null) {
            return mImageLoader;
        } else {
            throw new IllegalStateException("ImageLoader not initialized");
        }
    }

    public final void synCookies(Context context, String url) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.setCookie(url, "_w_count_=1; _w_count_date=2016/10/20; _w_today_count=0; _w_last_rule_time=1476960550; _w_today_pop_count=1; ASPSESSIONIDAAADDQQA=NKEJKMDAHKHPKOGIDNKKEKML; a7980_times=1; CNZZDATA1260110743=1344113117-1476953065-%7C1476953065; ASPSESSIONIDCAABBSRA=BIHPEOEAMOIFNGHPMPHHDFMM; wx=city4=0&city3=3824&city2=3822&city1=3821&city=0&apass=25D55AD283AA400AF464C76D713C07AD&auser=wlmq023&cookies=859739; allwww=opent=&openid=");//cookies是在HttpClient中获得的cookie
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.getInstance().sync();
        } else {
            CookieManager.getInstance().flush();
        }
    }
}
